package edu.yourl.web.components;

import java.util.ArrayList;
import java.util.List;

import org.apache.wicket.PageParameters;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.AbstractReadOnlyModel;

import edu.yourl.domain.Domain;
import edu.yourl.domain.Favoritable;
import edu.yourl.domain.FavoritableVisitor;
import edu.yourl.domain.Url;
import edu.yourl.domain.User;
import edu.yourl.web.DomainPage;
import edu.yourl.web.GossiperPage;
import edu.yourl.web.UrlPage;
import edu.yourl.web.UserPage;

public class FavoritableResultsPanel extends Panel {

	private static final long serialVersionUID = 1L;
	
	public FavoritableResultsPanel(String id, List<? extends Favoritable> listToShow) {
		this(id, listToShow,"No hay resultados");
	}
	
	public FavoritableResultsPanel(String id, List<? extends Favoritable> listToShow, String noResultsMessage) {
		super(id);
		WebMarkupContainer resultsContainer =  new WebMarkupContainer("resultsContainer");
		WebMarkupContainer noResultsContainer =  new WebMarkupContainer("noResultsContainer");
		if (listToShow == null) {
			listToShow = new ArrayList<Favoritable>();
		}
		resultsContainer.add(new ListView<Favoritable>("favoritableList",listToShow) {

			private static final long serialVersionUID = 1L;

			@SuppressWarnings("serial")
			@Override
			protected void populateItem(ListItem<Favoritable> item) {
				final Favoritable favoritable = item.getModelObject();
				Link<String> link = new Link<String>("link") {
					@Override
					public void onClick() {
						setResponsePage(pageSwitch(favoritable));
					}
				};
				link.add(new Label("text",favoritable.getFavoritableName()));
				item.add(link);
				
				
				item.add(new YourlImage("image", new ImageFavoritableModel(item.getModelObject())));
			}
		});
		
		noResultsContainer.add(new Label("noResultsLabel",noResultsMessage));
		
		noResultsContainer.setVisible(listToShow.isEmpty());
		resultsContainer.setVisible(!listToShow.isEmpty());
		
		add(resultsContainer);
		add(noResultsContainer);
		
	}
	
	private GossiperPage pageSwitch(Favoritable favoritable) {
		GossiperPage page = null;
		String seoFriendly = favoritable.getSEOFriendlyId();
		String keyValuePairs = GossiperPage.GOSSIPER_PARAMETER + "=" + seoFriendly;
		PageParameters parameters = new PageParameters(keyValuePairs);
		if (favoritable instanceof User) {
			page = new UserPage(parameters);
		} else if (favoritable instanceof Url) {
			page = new UrlPage(parameters);
		} else {
			page = new DomainPage(parameters);
		}
		return page;
	}
	
	
	private class ImageFavoritableModel extends AbstractReadOnlyModel<String> implements FavoritableVisitor<String> {
		
		private static final long serialVersionUID = 1L;

		private static final String IMG_DIR = "img/";

		Favoritable favorite;
		
		public ImageFavoritableModel(Favoritable favoritable) {
			this.favorite = favoritable;
		}
		
		@Override
		public String getObject() {
			return this.favorite.accept(this);
		}

		@Override
		public String visit(Domain domain) {
			return IMG_DIR+"domain.png";
		}

		@Override
		public String visit(Url url) {
			return IMG_DIR+"url.png";
		}

		@Override
		public String visit(User user) {
			return IMG_DIR+"user.png";
		}
	}
}
